import 'package:blog/base/get/controller/base_page_controller.dart';
import 'package:blog/model/music_card_model.dart';
import 'package:blog/model/project_model.dart';
import 'package:blog/ui/page/complex_module/music_page/widget/Music.dart';
import 'package:blog/util/ext/refresher_extension.dart';
import 'package:blog/widget/pull_smart_refresher.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';


/// @class : AskController
/// @date : 2021/09/02
/// @name : CTGU_LLZ(404name)
/// @description :问答 控制器层
class MusicController extends BaseGetPageController {
  List<MusicCardData> topCardList = [];
  List<MusicCardData> musicCardList = [];
  late AudioApp audioApp;
  int nowIdex = 0;
  List<MusicCardData> getList(){
    List<MusicCardData> temp = [];
    temp.add(new MusicCardData(
        musicUrl: "https://minio.404name.top/music/Florence.mp3",
        imgUrl: "http://p1.music.126.net/RSSgYT8T9XPMd_aPPZZXJQ==/109951163346110906.jpg",
        musicName: "Florence",
        musicDes: "第一首歌"));
    temp.add(new MusicCardData(
        musicUrl: "https://minio.404name.top/music/Kako - 春泥棒.mp3",
        imgUrl: "http://p1.music.126.net/kBtj7HlFDuokeO4iyHTXaA==/109951165616927101.jpg",
        musicName: "Kako - 春泥棒",
        musicDes: "第一首歌"));
    temp.add(new MusicCardData(
        musicUrl: "https://minio.404name.top/music/夏令时记录.mp3",
        imgUrl: "http://p1.music.126.net/mqYM_A9Y990c-eRVnaGUhg==/109951165050105840.jpg",
        musicName: "夏令时记录",
        musicDes: "夏令时记录"));
    temp.add(new MusicCardData(
        musicUrl: "https://minio.404name.top/music/One_Last_Kiss.mp3",
        imgUrl: "http://p2.music.126.net/l3G4LigZnOxFE9lB4bz_LQ==/109951165791860501.jpg",
        musicName: "One_Last_Kiss",
        musicDes: "One_Last_Kiss"));
    temp.add(new MusicCardData(
        musicUrl: "https://minio.404name.top/music/白日-King_Gnu.mp3",
        imgUrl: "http://p1.music.126.net/ZPnyNCu7uUUNW3MsuMpJqA==/109951165048502700.jpg",
        musicName: "白日-King_Gnu",
        musicDes: "白日-King_Gnu"));
    temp.add(new MusicCardData(
        musicUrl: "https://minio.404name.top/music/Lemon-米津玄师.mp3",
        imgUrl: "http://p2.music.126.net/jtPZRUFrSS-nRCjW_LYowQ==/109951166521931227.jpg",
        musicName: "Lemon-米津玄师",
        musicDes: "Lemon-米津玄师"));
    temp.add(new MusicCardData(
        musicUrl: "https://minio.404name.top/music/BOY-King_Gnu.mp3",
        imgUrl: "http://p2.music.126.net/qgVg7PrKXxivrUn3xZ1eUA==/109951166474269680.jpg",
        musicName: "BOY-King_Gnu",
        musicDes: "BOY-King_Gnu"));
    return temp;
  }
  @override
  void onInit() {
    super.onInit();
    topCardList = getList();
    audioApp = new AudioApp(
      title: topCardList[nowIdex].musicName,
      kUrl: topCardList[nowIdex].musicUrl,
      imgUrl: topCardList[nowIdex].imgUrl,
    );
    var audioPlayer2 = audioApp.createState().audioPlayer;
  }

  void updateMusic(){
    audioApp.createState().stop();
    audioApp = new AudioApp(
      title: topCardList[nowIdex].musicName,
      kUrl: topCardList[nowIdex].musicUrl,
      imgUrl: topCardList[nowIdex].imgUrl,
    );
    update();
  }

  ///请求积分明细
  @override
  void requestData(RefreshController controller,
      {Refresh refresh = Refresh.first}) {
    /*request.requestMusicCardModule(page, success: (data, over) {
      RefreshExtension.onSuccess(controller, refresh, over);
      ///下拉刷新需要清除列表
      if (refresh != Refresh.down) {
        musicCardList.clear();
      }
      musicCardList.addAll(data);
      showSuccess(musicCardList);
      update();
    }, fail: (code, msg) {
      showError();
      RefreshExtension.onError(controller, refresh);
    });*/
    request.requestAskModule(page, success: (data, over) {
      // 模拟加数据
      ///下拉刷新需要清除列表
      print("开始判断");
      RefreshExtension.onSuccess(controller, refresh, false);
      if (refresh != Refresh.down) {
        print("上拉");
        musicCardList.clear();
      }
      musicCardList.addAll(getList());
      showSuccess(musicCardList);
      update();
    });
  }
}
